package com.example.project_01

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.example.project_01.model.LoginRequest
import com.example.project_01.utils.RetrofitClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class HttpActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_http)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        // 1. 获取频道列表
        findViewById<Button>(R.id.getChannel).setOnClickListener {
            try {
                // 启动一个新的协程，用于处理获取频道列表的异步网络请求
                lifecycleScope.launch(context = Dispatchers.Main) {
                    // 使用 withContext 切换到 IO 线程，调用 Retrofit 接口获取频道列表数据
                    val res = withContext(Dispatchers.IO) {
                        // 调用 RetrofitClient 的 ApiService 获取频道数据
                        RetrofitClient.apiService.getChannels()
                    }
                    if (res.code == 10000) {
                        // 处理响应
                        val data = res.data?.channels
                        findViewById<TextView>(R.id.channel).text =
                            "获取频道列表成功, 频道名称: ${data?.joinToString(", ") { it.name }}"
                    } else {
                        // 处理响应
                        Toast.makeText(this@HttpActivity, "获取频道列表失败", Toast.LENGTH_SHORT)
                            .show()
                    }
                }
            } catch (e: Exception) {
                // 处理错误
                Toast.makeText(
                    this@HttpActivity,
                    "请求失败",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }

        // 2. 登录操作
        findViewById<Button>(R.id.login).setOnClickListener {
            try {
                lifecycleScope.launch {
                    val req = LoginRequest("admin", "123456")
                    val res = withContext(Dispatchers.IO) {
                        RetrofitClient.apiService.login(req)
                    }

                    if (res.code == 10000) {
                        val token = res.data?.token
                        findViewById<TextView>(R.id.tv).text = "登录成功, Token: $token"
                    } else {
                        // 处理响应
                        Toast.makeText(
                            this@HttpActivity,
                            "登录失败",
                            Toast.LENGTH_SHORT
                        ).show()
                    }

                }
            } catch (e: Exception) {
                // 处理错误
                Toast.makeText(
                    this@HttpActivity,
                    "请求失败",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }
}